package com.zy.service.bs.impl;

import com.zy.dao.bs.CarDealerDao;
import com.zy.entity.bs.CarDealer;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.utils.tree.BaseTree;
import com.zy.utils.tree.CarTypeTree;
import com.zy.utils.tree.TreeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zy.dao.bs.CarTypeDao;
import com.zy.entity.enums.StatusEnum;
import com.zy.entity.bs.CarType;
import com.zy.pojo.query.bs.CarTypeQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.CarTypeService;
import com.zy.utils.DateUtil;
import tk.mybatis.mapper.entity.Example;
import java.util.ArrayList;
import java.util.List;

@Service("carTypeService")
public class CarTypeServiceImpl extends BaseServiceImpl<CarType> implements CarTypeService {

	@Autowired
	private CarTypeDao carTypeDao;
	@Autowired
	private CarDealerDao carDealerDao;

	@Override
	public PageVo pageCarType(CarTypeQuery carTypeQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.carTypeDao.countCarType(carTypeQuery), this.carTypeDao.listCarType(carTypeQuery, start, limit));
	}

	@Override
	public void saveCarType(CarType carType) {
		carType.setId(null);
		existsName(carType);
		long time = DateUtil.getTime();
		carType.setCreateTime(time);
		carType.setModifyTime(time);
		carType.setStatus(StatusEnum.NORMAL.getValue());
		this.carTypeDao.save(carType);
	}
	
	@Override
	public void updateCarType(CarType carType) {
		existsName(carType);
		carType.setModifyTime(DateUtil.getTime());
		this.carTypeDao.updateByPrimaryKeySelective(carType);
	}

	@Override
	public void removeCarType(long id) {
		try {
			this.carTypeDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			CarType carType = new CarType();
			carType.setId(id);
			carType.setModifyTime(DateUtil.getTime());
			carType.setStatus(StatusEnum.DELETE.getValue());
			this.carTypeDao.updateByPrimaryKeySelective(carType);
		}
	}

	@Override
	public void recover(long id) {
		CarType carType = new CarType();
		carType.setId(id);
		carType.setModifyTime(DateUtil.getTime());
		carType.setStatus(StatusEnum.NORMAL.getValue());
		this.carTypeDao.updateByPrimaryKeySelective(carType);
	}

    @Override
    public BaseTree carType() {

        BaseTree root = new BaseTree();
        root.setId("0");
        root.setParentId("-1");
        List<BaseTree> rootNode = new ArrayList<BaseTree>();
        List<CarDealer> carDealers = carDealerDao.listAll();
        carDealers.forEach(carDealer -> {
            BaseTree node = new BaseTree();
            node.setId(carDealer.getId()+"");
            node.setParentId("0");
            node.setCarDelaerName(carDealer.getName());
            Example ex = new Example(CarType.class);
            ex.createCriteria().andEqualTo("carDealerId",carDealer.getId());
            List<CarType> cartypes = carTypeDao.listByExample(ex);
            List<BaseTree> listTree = new ArrayList<>();
            cartypes.forEach(carType -> {

                BaseTree tree= new BaseTree();
                tree.setCarTypeName(carType.getName());
                tree.setId(carType.getId()+"");
                listTree.add(tree);
            });
            node.setChildren(listTree);
            rootNode.add(node);
        });
        TreeUtil.appendChildren(root, rootNode);
        return root;
    }

    private void existsName(CarType carType) {
		Example ex = new Example(CarType.class);
		Example.Criteria cr = ex.createCriteria();
		cr.andEqualTo("name", carType.getName()).andEqualTo("carDealerId",carType.getCarDealerId());
		CarType pojo = carTypeDao.getByExample(ex);
		if (pojo != null && !pojo.getId().equals(carType.getId())) {
			throw new ServiceException(carType.getName()+"重复车系名");
		}

	}

}
